<?php

/**
 * 分类管理
 */

$title = '分类管理';
include "header.php";
$sum = 8; #每页数量
$page = shop::page($sum, $_QET['page']); #取出翻页数据

$category_list = shop::category_list($sum, $page['count'], $page['limit']); #获取分类列表
$paging = shop::paging($category_list['count'], $_QET['page']); #取出翻页按钮数据
if (!empty($_QET['unset'])) {
    $s = $DB->get_row("SELECT cid,name FROM `sky_class` WHERE `cid`='{$_QET['unset']}' ");
    if (!$s) show_msg('分类不存在', '您想删除的分类[' . $_QET['unset'] . ']不存在！');

    $a = $DB->query("DELETE FROM `sky_class` WHERE (`cid`='{$_QET['unset']}')"); #删除
    $a = $DB->query("DELETE FROM `sky_goods` WHERE (`cid`='{$_QET['unset']}')"); #删除
    if ($a) {
        \lib\Hook\Hook::execute('ClassDel', ['cid' => $_QET['unset'], 'name' => $s['name']]);
        show_msg('成功提示', '分类:' . $s['name'] . '删除成功,此分类下的商品也全部删除,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '分类:' . $s['name'] . '删除失败,原因->' . $DB->error(), '2');
}
?>
<!-- third party css -->
<link href="<?= $cdnserver == null ? '../' : $cdnserver ?>assets/css/vendor/dataTables.bootstrap4.css" rel="stylesheet" type="text/css" />
<link href="<?= $cdnserver == null ? '../' : $cdnserver ?>assets/css/vendor/responsive.bootstrap4.css" rel="stylesheet" type="text/css" />
<!-- third party css end -->
<!-- App css -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4">
                        <a href="admin.class.add.php" class="btn btn-danger mb-2"><i class="layui-icon layui-icon-add-circle-fine mr-2"></i>
                            新增分类
                        </a>
                        <a href="admin.goods.list.php" class="btn btn-success mb-2"><i class="layui-icon layui-icon-light mr-2"></i>
                            查看商品
                        </a>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-centered table-striped dt-responsive nowrap w-100" id="products-datatable">
                        <thead>
                            <tr style="white-space: nowrap">
                                <th>CID</th>
                                <th>分类排序</th>
                                <th>分类名称</th>
                                <th>商品数量</th>
                                <th>分类图片</th>
                                <th>分类状态</th>
                                <th>支持付款方式</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($category_list['data'] as $v) {
                            ?>
                                <tr style="white-space: nowrap;font-size: 0.9em">
                                    <td>
                                        <a href="admin.class.add.php?cid=<?= $v['cid'] ?>" class="badge badge-info-lighten">
                                            <?= $v['cid'] ?>
                                        </a>
                                    </td>
                                    <td class="table-user">
                                        <?= $v['sort'] ?>
                                    </td>
                                    <td>
                                        <a href="../?cid=<?= $v['cid'] ?>" target="_blank" style="color: red" title="打开分类"><?= $v['name'] ?></a>
                                    </td>
                                    <td>
                                        <a href="admin.goods.list.php?cid=<?= $v['cid'] ?>" title="查看分类[<?= $v['name'] ?>]下的<?= $v['count']; ?>个商品"><?= $v['count']; ?>
                                            个商品</a>
                                    </td>
                                    <td>
                                        <img src="<?= ImageUrl($v['image']) ?>" style="width:4em" />
                                    </td>
                                    <td>
                                        <?= $v['state'] ?>
                                    </td>
                                    <td>
                                        <?= $v['support'] ?>
                                    </td>
                                    <td>
                                        <?= $v['date'] ?>
                                    </td>
                                    <td>
                                        <a href="admin.class.add.php?cid=<?= $v['cid'] ?>" class="action-icon"> <i class="layui-icon layui-icon-set-fill"></i></a>
                                        <a href="javascript:unset(<?= $v['cid'] ?>)" class="action-icon"> <i class="layui-icon layui-icon-delete"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>

                        </tbody>
                    </table>
                </div>
                <nav>
                    <ul class="pagination pagination-rounded" style="overflow: auto">
                        <li class="page-item">
                            <a class="page-link" href="?page=1" aria-label="Previous">
                                <span aria-hidden="true">«</span>
                                <span class="sr-only">Previous</span>
                            </a>
                        </li>
                        <?php foreach ($paging as $v) { ?>
                            <li class="page-item <?= $v['active'] ?>"><a class="page-link" href="?page=<?= $v['number'] ?>"><?= $v['number'] ?></a>
                            </li>
                        <?php } ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?= $category_list['count'] ?>" aria-label="Next">
                                <span aria-hidden="true">»</span>
                                <span class="sr-only">Next</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div> <!-- end card-body-->
        </div> <!-- end card-->
    </div> <!-- end col -->
</div>


<?php include "bottom.php"; ?>
<script>
    function unset(a) {
        layer.alert('删除后不可撤销，是否确认删除？<br>删除分类将会删除分类下的商品!', {
            icon: 3,
            title: '温馨提示',
            btn: ['取消', '确定删除'],
            btn2: function(layero, index) {
                window.location.href = '?unset=' + a;
            }
        })
    }

    function sort(cid, type) {
        $.ajax({
            type: 'POST',
            url: 'ajax.php?act=setClassSort',
            data: {
                cid: cid,
                type: type
            },
            dataType: 'json',
            success: function(data) {
                location.reload();
            },
            error: function(data) {
                layer.msg('服务器错误');
                return false;
            }
        });
    }
</script>